/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cloudbus.cloudsim.power.models.scheduling;

import java.util.ArrayList;
import java.util.List;
import org.cloudbus.cloudsim.Vm;
import org.cloudbus.cloudsim.Cloudlet;

/**
 *
 * @author Márcio Costa Junior - University of Sao Paulo, Montana State University
 */
public class Middleware {
    
    private ArrayList<ClassMid> classList;
    private int numberClasses;
    private int id;
    private long costs;
        
    public Middleware(int numberClasses, double mipsBarrier, int id){
        
        setNumberClasses(numberClasses);
        setClassList(new ArrayList<ClassMid>());
        
        int barrier = (int) (mipsBarrier/numberClasses);
        int lastBarrier = 0;
        
        for(int i = 0; i < numberClasses; i++){
            getClassList().add(new ClassMid(lastBarrier, lastBarrier + barrier, i));
            lastBarrier += barrier;
        }
        this.id = id;
        
    }
    
    //Returns the appropriated Front-End 
    public FrontEnd filterRequest(int service){
        
        return getClassList().get(service).getFrontEnd();
    }
    
    public void createFrontEnds(int numberHosts, int numberPe){
        
            for(ClassMid group : getClassList()){
                group.createFrontEnd(numberHosts, numberPe);
            }
        
    }
    
    public void createLocalDatacenters(int mips, int ram, long storage, int bw, double timeZone, double peCost, 
            long costPerMem, long costPerStorage, long costPerBw, double pePotency, double powerPercent,
            double processingCost, String os, String arch, String vmm){
        
            for(ClassMid group : getClassList()){
                group.createLocalDatacenter(mips, ram, storage, bw, timeZone, peCost, 
                        costPerMem, costPerStorage, 
                        costPerBw, pePotency, powerPercent, processingCost, os, arch, vmm);
            }
        
    }
    
    public void submitRequestList(List<List<Vm>> vmList, List<List<Cloudlet>> cloudletList){
        
            for(int i = 0; i < getClassList().size(); i++){
                getClassList().get(i).getFrontEnd().submitVmList(vmList.get(i));
                getClassList().get(i).getFrontEnd().submitCloudletList(cloudletList.get(i));
        }
        
    }
    
   
    public void createEstimators(List<List<Vm>> vmList){
        
        for(int i = 0; i < getClassList().size(); i++){
            getClassList().get(i).createEstimator(vmList.get(i).size());
        }
       
    }
        
    
    

    /**
     * @return the classList
     */
    public ArrayList<ClassMid> getClassList() {
        return classList;
    }

    /**
     * @param classList the classList to set
     */
    public void setClassList(ArrayList<ClassMid> classList) {
        this.classList = classList;
    }

    /**
     * @return the numberClasses
     */
    public int getNumberClasses() {
        return numberClasses;
    }


    /**
     * @param numberClasses the numberClasses to set
     */
    public void setNumberClasses(int numberClasses) {
        this.numberClasses = numberClasses;
    }

    /**
     * @return the costs
     */
    public long getCosts() {
        return costs;
    }
    
    public void setCosts(long costs){
        this.costs = costs;
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }
  
}
